<?php
class DbConnection {
	/**
	 * 
	 * create a database connection
	 */
	private $_con;
		
	public function connect() {
		//create a connection to the database			
		global $ubi;	
		$this->_con = mysql_connect($ubi['DB']['HOST'], $ubi['DB']['USER'], $ubi['DB']['PASSWORD']);
						
		if ( !$this->_con )
			die( "Couldn't connect to MySQL" );
		//use database, neu co loi, wang loi
		mysql_select_db($ubi['DB']['NAME'], $this->_con) or die ( "Couldn't open " . $ubi['DB']['NAME'] . " " . mysql_error($this->_con) );
		mysql_query("SET character_set_results=utf8", $this->_con);
		mysql_query("SET NAMES 'utf8'", $this->_con);

		return $this->_con;
	}
	
	public function close() {
		if ($this->_con) {

		}
	}
	
	public function __destruct() {
		$this->close();
	}
	
	public function getConnection() {
		if ($this->_con)
			return $this->_con;
			
		return $this->connect();
	}	
	
	public function executeQuery($query) {		
		$result = mysql_query($query, $this->getConnection());
		
		return $result;
	}
}
?>